package com.findtech.threePomelos.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.findtech.threePomelos.R;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.database.OperateDBUtils;
import com.findtech.threePomelos.entity.TravelInfoEntity;
import com.findtech.threePomelos.net.NetWorkRequest;
import com.findtech.threePomelos.utils.RequestUtils;

import java.util.Date;
import java.util.List;

public class MileageTipsFragment extends Fragment {

    private float sixDayMileage = 0;//计算今天之前6天的总里程
    private final int QUERY_FINISH_MSG = 0x66;
    private TextView mileageHint;
    private TravelInfoEntity travelInfoEntity = TravelInfoEntity.getInstance();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mileage_tips, container, false);
        querySexDayMileage();
        mileageHint = (TextView) view.findViewById(R.id.mileage_hint);
        String hint = RequestUtils.getSharepreference(getContext()).getString(RequestUtils.MILEAGE_HINT, "");
        mileageHint.setText(hint);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void querySexDayMileage() {
        AVQuery<AVObject> query = new AVQuery<>(NetWorkRequest.TRAVEL_INFO);
        Date now = new Date();
        query.whereEqualTo("post", AVUser.getCurrentUser());
        query.whereLessThanOrEqualTo("createdAt", now);//查询今天之前创建的
        query.orderByAscending(OperateDBUtils.DATE);
        query.limit(6);// 最多返回 7 条结果
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null) {
                    Log.d("ZZ", "list.size() = " + list.size());
                    for (int i = 0; i < list.size(); i++) {
                        AVObject avObject = list.get(i);
                        String todayMileageStr = avObject.getString(OperateDBUtils.TODAY_MILEAGE);
                        if (!TextUtils.isEmpty(todayMileageStr)) {
                            sixDayMileage += Float.parseFloat(todayMileageStr);
                        }
                    }
                    mHandler.obtainMessage(QUERY_FINISH_MSG, sixDayMileage).sendToTarget();
                } else {
                    mHandler.obtainMessage(QUERY_FINISH_MSG, 0).sendToTarget();
                }
            }
        });
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == QUERY_FINISH_MSG) {
                if (isAdded()) {
                    msg.arg1 += Float.parseFloat(travelInfoEntity.getTodayMileage());
                    if (msg.arg1 >= 10) {
                        mileageHint.setText(getResources().getString(R.string.mileage_tips_fragment_more_than_10));
                        RequestUtils.getSharepreferenceEditor(getContext()).putString(RequestUtils.MILEAGE_HINT,
                                getResources().getString(R.string.mileage_tips_fragment_more_than_10)).commit();
                    } else if (msg.arg1 >= 5 && msg.arg1 < 10) {
                        mileageHint.setText(getResources().getString(R.string.mileage_tips_fragment_more_than_5));
                        RequestUtils.getSharepreferenceEditor(getContext()).putString(RequestUtils.MILEAGE_HINT,
                                getResources().getString(R.string.mileage_tips_fragment_more_than_5)).apply();
                    } else {
                        mileageHint.setText(getResources().getString(R.string.mileage_tips_fragment_less_than_5));
                        RequestUtils.getSharepreferenceEditor(getContext()).putString(RequestUtils.MILEAGE_HINT,
                                getResources().getString(R.string.mileage_tips_fragment_less_than_5)).commit();
                    }
                }
            }

        }
    };

}
